﻿// <copyright file="HtmlToEnumerableConverter.cs" company="Microsoft Corporation">
// Copyright (c) 2009 Microsoft Corporation All Rights Reserved
// </copyright>
// <author>Michael S. Scherotter</author>
// <email>mischero@microsoft.com</email>
// <date>2009-03-03</date>
// <summary>A Value Converter</summary>

namespace MyOpenSpace
{
    using System;
    using System.Collections.Generic;
    using System.Linq;
    using System.Windows;
    using System.Windows.Data;
    using System.Xml.Linq;

    /// <summary>
    /// A Value converter
    /// </summary>
    public class HtmlToEnumerableConverter : IValueConverter
    {
        #region IValueConverter Members

        /// <summary>
        /// Modifies the source data before passing it to the target for display in the UI. 
        /// </summary>
        /// <param name="value">The source data being passed to the target </param>
        /// <param name="targetType">The Type of data expected by the target dependency property.</param>
        /// <param name="parameter">An optional parameter to be used in the converter logic.</param>
        /// <param name="culture">The culture of the conversion.</param>
        /// <returns>The value to be passed to the target dependency property. </returns>
        public object Convert(object value, Type targetType, object parameter, System.Globalization.CultureInfo culture)
        {
            if (value == null)
            {
                return null;
            }

            var collection = value as IEnumerable<string>;

            if (collection.Count() == 1)
            {
                var firstItem = collection.ElementAt(0);

                try
                {
                    // First try to parse the items are listed with <li> elements
                    var doc = XDocument.Parse(firstItem);

                    var listItems = doc.Descendants("li");

                    var items = from listItem in listItems
                                select listItem.Value.Trim();

                    return items;
                }
                catch (System.Xml.XmlException)
                {
                    // Then try to parse the items with newline characters
                    if (firstItem.Contains("\r\n"))
                    {
                        return firstItem.Split(new string[] { "\r\n" }, StringSplitOptions.RemoveEmptyEntries);
                    }
                }
            }

            return collection;
        }

        /// <summary>
        /// Modifies the target data before passing it to the source object. This method is called only in TwoWay bindings. 
        /// </summary>
        /// <param name="value">The target data being passed to the source.</param>
        /// <param name="targetType">The Type of data expected by the source object.</param>
        /// <param name="parameter">An optional parameter to be used in the converter logic. </param>
        /// <param name="culture">The culture of the conversion.</param>
        /// <returns>The value to be passed to the source object.</returns>
        public object ConvertBack(object value, Type targetType, object parameter, System.Globalization.CultureInfo culture)
        {
            return DependencyProperty.UnsetValue;
        }

        #endregion
    }
}
